<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>分发脚本 ansible | 皮卡车</title>
    <meta name="description" content="皮卡车的文档, vuepress 文档">
    <meta name="generator" content="VuePress 1.3.1">
    <link rel="apple-touch-icon" href="/apple-touch-icon.png">
  <link rel="icon" href="/favicon.ico">
  <link rel="manifest" href="/manifest.json">
  <meta name="theme-color" content="#ffffff">
  <meta name="google-site-verification" content="Ld_AEWr30siza2LmmZ12csitRGsSj9gqqEAch0UXkTc">
    
    <link rel="preload" href="/assets/css/0.styles.1a6a4f51.css" as="style"><link rel="preload" href="/assets/js/app.1f0f93c9.js" as="script"><link rel="preload" href="/assets/js/2.f28f3227.js" as="script"><link rel="preload" href="/assets/js/97.65a03aad.js" as="script"><link rel="preload" href="/assets/js/3.a7eaa85f.js" as="script"><link rel="prefetch" href="/assets/js/10.7b76d38f.js"><link rel="prefetch" href="/assets/js/100.0d62a3f6.js"><link rel="prefetch" href="/assets/js/101.4916ac0c.js"><link rel="prefetch" href="/assets/js/102.0ee21f66.js"><link rel="prefetch" href="/assets/js/103.872c594c.js"><link rel="prefetch" href="/assets/js/104.0bff815e.js"><link rel="prefetch" href="/assets/js/105.46330c43.js"><link rel="prefetch" href="/assets/js/106.4ad211ca.js"><link rel="prefetch" href="/assets/js/107.d9011871.js"><link rel="prefetch" href="/assets/js/108.bab8ed0e.js"><link rel="prefetch" href="/assets/js/109.b7e4a790.js"><link rel="prefetch" href="/assets/js/11.518e0dbb.js"><link rel="prefetch" href="/assets/js/110.1eb492e8.js"><link rel="prefetch" href="/assets/js/111.ee6a2e31.js"><link rel="prefetch" href="/assets/js/112.05d655c6.js"><link rel="prefetch" href="/assets/js/113.0a4cf8f0.js"><link rel="prefetch" href="/assets/js/114.ef783947.js"><link rel="prefetch" href="/assets/js/115.11bbdb9c.js"><link rel="prefetch" href="/assets/js/116.5646126d.js"><link rel="prefetch" href="/assets/js/117.854d26bf.js"><link rel="prefetch" href="/assets/js/118.1e514b13.js"><link rel="prefetch" href="/assets/js/119.aa944f45.js"><link rel="prefetch" href="/assets/js/12.511e3547.js"><link rel="prefetch" href="/assets/js/120.1c6f450c.js"><link rel="prefetch" href="/assets/js/121.44f16ee6.js"><link rel="prefetch" href="/assets/js/122.f877b8e8.js"><link rel="prefetch" href="/assets/js/123.0563fdd5.js"><link rel="prefetch" href="/assets/js/124.4cd97316.js"><link rel="prefetch" href="/assets/js/125.a502f4ee.js"><link rel="prefetch" href="/assets/js/126.c7e94093.js"><link rel="prefetch" href="/assets/js/127.bd8aed7f.js"><link rel="prefetch" href="/assets/js/128.66063cc3.js"><link rel="prefetch" href="/assets/js/129.21bb1073.js"><link rel="prefetch" href="/assets/js/13.9e5bee4e.js"><link rel="prefetch" href="/assets/js/130.c8edfccb.js"><link rel="prefetch" href="/assets/js/131.7a1f29be.js"><link rel="prefetch" href="/assets/js/132.f589fc06.js"><link rel="prefetch" href="/assets/js/133.55ac5c9a.js"><link rel="prefetch" href="/assets/js/134.eeab127b.js"><link rel="prefetch" href="/assets/js/135.5a1571dd.js"><link rel="prefetch" href="/assets/js/136.b72b4d1c.js"><link rel="prefetch" href="/assets/js/137.678e022a.js"><link rel="prefetch" href="/assets/js/138.ba4108fa.js"><link rel="prefetch" href="/assets/js/139.0b02b599.js"><link rel="prefetch" href="/assets/js/14.b9ec07e6.js"><link rel="prefetch" href="/assets/js/140.690e4e4c.js"><link rel="prefetch" href="/assets/js/141.709da4ed.js"><link rel="prefetch" href="/assets/js/142.f2de76c0.js"><link rel="prefetch" href="/assets/js/143.4dcb17f0.js"><link rel="prefetch" href="/assets/js/144.730ef429.js"><link rel="prefetch" href="/assets/js/145.d881c754.js"><link rel="prefetch" href="/assets/js/146.f3379a41.js"><link rel="prefetch" href="/assets/js/147.eeb9ca6f.js"><link rel="prefetch" href="/assets/js/148.bb6020cd.js"><link rel="prefetch" href="/assets/js/149.1d34dea6.js"><link rel="prefetch" href="/assets/js/15.d0fa1a9d.js"><link rel="prefetch" href="/assets/js/150.599238ba.js"><link rel="prefetch" href="/assets/js/151.9655268f.js"><link rel="prefetch" href="/assets/js/152.1f843fa4.js"><link rel="prefetch" href="/assets/js/153.7af45ca6.js"><link rel="prefetch" href="/assets/js/154.eff93a56.js"><link rel="prefetch" href="/assets/js/155.ff07fba4.js"><link rel="prefetch" href="/assets/js/156.1b1814b7.js"><link rel="prefetch" href="/assets/js/157.8ac4c21b.js"><link rel="prefetch" href="/assets/js/158.aa8a0124.js"><link rel="prefetch" href="/assets/js/159.d61fdb37.js"><link rel="prefetch" href="/assets/js/16.ccd2bd60.js"><link rel="prefetch" href="/assets/js/17.ddc8fde4.js"><link rel="prefetch" href="/assets/js/18.a06e60cc.js"><link rel="prefetch" href="/assets/js/19.64b3dec9.js"><link rel="prefetch" href="/assets/js/20.3615e182.js"><link rel="prefetch" href="/assets/js/21.0c654d53.js"><link rel="prefetch" href="/assets/js/22.720aac8f.js"><link rel="prefetch" href="/assets/js/23.0dc53b91.js"><link rel="prefetch" href="/assets/js/24.d4b75b0e.js"><link rel="prefetch" href="/assets/js/25.9120702e.js"><link rel="prefetch" href="/assets/js/26.b24d89e2.js"><link rel="prefetch" href="/assets/js/27.55df94ab.js"><link rel="prefetch" href="/assets/js/28.250c68a5.js"><link rel="prefetch" href="/assets/js/29.75a3c8f3.js"><link rel="prefetch" href="/assets/js/30.8cdc2fcd.js"><link rel="prefetch" href="/assets/js/31.45303d68.js"><link rel="prefetch" href="/assets/js/32.f4225739.js"><link rel="prefetch" href="/assets/js/33.15b21267.js"><link rel="prefetch" href="/assets/js/34.5baca30b.js"><link rel="prefetch" href="/assets/js/35.5d9dfab1.js"><link rel="prefetch" href="/assets/js/36.981eadca.js"><link rel="prefetch" href="/assets/js/37.cead7f6b.js"><link rel="prefetch" href="/assets/js/38.ee6bcaf1.js"><link rel="prefetch" href="/assets/js/39.e7579788.js"><link rel="prefetch" href="/assets/js/4.494c614b.js"><link rel="prefetch" href="/assets/js/40.01d2b30b.js"><link rel="prefetch" href="/assets/js/41.19cf06ce.js"><link rel="prefetch" href="/assets/js/42.15941cde.js"><link rel="prefetch" href="/assets/js/43.66adb73c.js"><link rel="prefetch" href="/assets/js/44.8d99ed62.js"><link rel="prefetch" href="/assets/js/45.12ea9159.js"><link rel="prefetch" href="/assets/js/46.644efa88.js"><link rel="prefetch" href="/assets/js/47.531131c7.js"><link rel="prefetch" href="/assets/js/48.b214f000.js"><link rel="prefetch" href="/assets/js/49.e42d4b72.js"><link rel="prefetch" href="/assets/js/5.7e277dc8.js"><link rel="prefetch" href="/assets/js/50.de0aa85c.js"><link rel="prefetch" href="/assets/js/51.8be2974c.js"><link rel="prefetch" href="/assets/js/52.cc067888.js"><link rel="prefetch" href="/assets/js/53.4ceb2c6b.js"><link rel="prefetch" href="/assets/js/54.b2647058.js"><link rel="prefetch" href="/assets/js/55.dc3df953.js"><link rel="prefetch" href="/assets/js/56.921a6934.js"><link rel="prefetch" href="/assets/js/57.1e24b1fd.js"><link rel="prefetch" href="/assets/js/58.f4e8ebac.js"><link rel="prefetch" href="/assets/js/59.17ab117b.js"><link rel="prefetch" href="/assets/js/6.4014218f.js"><link rel="prefetch" href="/assets/js/60.352cea9b.js"><link rel="prefetch" href="/assets/js/61.77289388.js"><link rel="prefetch" href="/assets/js/62.ec0ed919.js"><link rel="prefetch" href="/assets/js/63.c75dcb25.js"><link rel="prefetch" href="/assets/js/64.59d15a75.js"><link rel="prefetch" href="/assets/js/65.7e5a14bb.js"><link rel="prefetch" href="/assets/js/66.4e770ba1.js"><link rel="prefetch" href="/assets/js/67.0742f21a.js"><link rel="prefetch" href="/assets/js/68.a6ed34fd.js"><link rel="prefetch" href="/assets/js/69.48c9a47f.js"><link rel="prefetch" href="/assets/js/7.c5ecd7d3.js"><link rel="prefetch" href="/assets/js/70.eb477192.js"><link rel="prefetch" href="/assets/js/71.1be57009.js"><link rel="prefetch" href="/assets/js/72.6859561c.js"><link rel="prefetch" href="/assets/js/73.aae65018.js"><link rel="prefetch" href="/assets/js/74.59d76d34.js"><link rel="prefetch" href="/assets/js/75.04e23427.js"><link rel="prefetch" href="/assets/js/76.74d6eb6a.js"><link rel="prefetch" href="/assets/js/77.fba76057.js"><link rel="prefetch" href="/assets/js/78.0da94b25.js"><link rel="prefetch" href="/assets/js/79.14676976.js"><link rel="prefetch" href="/assets/js/8.08c85334.js"><link rel="prefetch" href="/assets/js/80.7494721f.js"><link rel="prefetch" href="/assets/js/81.f04f6229.js"><link rel="prefetch" href="/assets/js/82.e4c6f74b.js"><link rel="prefetch" href="/assets/js/83.81ad8dbf.js"><link rel="prefetch" href="/assets/js/84.d28e4a3d.js"><link rel="prefetch" href="/assets/js/85.d99fa7fa.js"><link rel="prefetch" href="/assets/js/86.597ecadb.js"><link rel="prefetch" href="/assets/js/87.e6d21734.js"><link rel="prefetch" href="/assets/js/88.b43a52d4.js"><link rel="prefetch" href="/assets/js/89.a5ba287d.js"><link rel="prefetch" href="/assets/js/9.658cd6b3.js"><link rel="prefetch" href="/assets/js/90.c253df12.js"><link rel="prefetch" href="/assets/js/91.8946a93d.js"><link rel="prefetch" href="/assets/js/92.8b8f1ae0.js"><link rel="prefetch" href="/assets/js/93.aa73c784.js"><link rel="prefetch" href="/assets/js/94.9326bcc7.js"><link rel="prefetch" href="/assets/js/95.46b64b79.js"><link rel="prefetch" href="/assets/js/96.61a9b0b9.js"><link rel="prefetch" href="/assets/js/98.7aa56ed0.js"><link rel="prefetch" href="/assets/js/99.a652c8d5.js">
    <link rel="stylesheet" href="/assets/css/0.styles.1a6a4f51.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">皮卡车</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  Home
</a></div><div class="nav-item"><a href="/guide/" class="nav-link">
  Guide
</a></div><div class="nav-item"><a href="/computer/" class="nav-link">
  计算机
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="系统" class="dropdown-title"><span class="title">系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/os/linux/" class="nav-link router-link-active">
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/os/manjaro/" class="nav-link">
  Manjaro
</a></li><li class="dropdown-item"><!----> <a href="/os/ubuntu/" class="nav-link">
  Ubuntu
</a></li><li class="dropdown-item"><!----> <a href="/os/centos/" class="nav-link">
  CentOS
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><span class="title">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/frontend/javascript/" class="nav-link">
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="https://css.shanyuhai.top/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSS
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="/frontend/webpack/" class="nav-link">
  Webpack
</a></li><li class="dropdown-item"><!----> <a href="/frontend/d3js/" class="nav-link">
  D3
</a></li><li class="dropdown-item"><!----> <a href="/frontend/utils/" class="nav-link">
  Utils
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="后端" class="dropdown-title"><span class="title">后端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/backend/nodejs/" class="nav-link">
  Nodejs
</a></li><li class="dropdown-item"><!----> <a href="/backend/koa/" class="nav-link">
  Koa
</a></li><li class="dropdown-item"><!----> <a href="/backend/mongodb/" class="nav-link">
  MongoDB
</a></li><li class="dropdown-item"><!----> <a href="/backend/nginx/" class="nav-link">
  Nginx
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开发工具" class="dropdown-title"><span class="title">开发工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/tools/git/" class="nav-link">
  Git
</a></li><li class="dropdown-item"><!----> <a href="/tools/github/" class="nav-link">
  Github
</a></li><li class="dropdown-item"><!----> <a href="/tools/vscode/" class="nav-link">
  VSCode
</a></li><li class="dropdown-item"><!----> <a href="/tools/chrome/" class="nav-link">
  Chrome Developer tools
</a></li><li class="dropdown-item"><!----> <a href="/tools/bookmark-scripts/" class="nav-link">
  Bookmark scripts
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><span class="title">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/more/algorithm/" class="nav-link">
  算法
</a></li><li class="dropdown-item"><!----> <a href="/more/interview/" class="nav-link">
  面试题
</a></li><li class="dropdown-item"><!----> <a href="/more/hodgepodge/" class="nav-link">
  大杂烩
</a></li><li class="dropdown-item"><!----> <a href="/more/clean/" class="nav-link">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="https://v1.vuepress.vuejs.org/zh/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  VuePress1.x 官网
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  Home
</a></div><div class="nav-item"><a href="/guide/" class="nav-link">
  Guide
</a></div><div class="nav-item"><a href="/computer/" class="nav-link">
  计算机
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="系统" class="dropdown-title"><span class="title">系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/os/linux/" class="nav-link router-link-active">
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/os/manjaro/" class="nav-link">
  Manjaro
</a></li><li class="dropdown-item"><!----> <a href="/os/ubuntu/" class="nav-link">
  Ubuntu
</a></li><li class="dropdown-item"><!----> <a href="/os/centos/" class="nav-link">
  CentOS
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><span class="title">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/frontend/javascript/" class="nav-link">
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="https://css.shanyuhai.top/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSS
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="/frontend/webpack/" class="nav-link">
  Webpack
</a></li><li class="dropdown-item"><!----> <a href="/frontend/d3js/" class="nav-link">
  D3
</a></li><li class="dropdown-item"><!----> <a href="/frontend/utils/" class="nav-link">
  Utils
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="后端" class="dropdown-title"><span class="title">后端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/backend/nodejs/" class="nav-link">
  Nodejs
</a></li><li class="dropdown-item"><!----> <a href="/backend/koa/" class="nav-link">
  Koa
</a></li><li class="dropdown-item"><!----> <a href="/backend/mongodb/" class="nav-link">
  MongoDB
</a></li><li class="dropdown-item"><!----> <a href="/backend/nginx/" class="nav-link">
  Nginx
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开发工具" class="dropdown-title"><span class="title">开发工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/tools/git/" class="nav-link">
  Git
</a></li><li class="dropdown-item"><!----> <a href="/tools/github/" class="nav-link">
  Github
</a></li><li class="dropdown-item"><!----> <a href="/tools/vscode/" class="nav-link">
  VSCode
</a></li><li class="dropdown-item"><!----> <a href="/tools/chrome/" class="nav-link">
  Chrome Developer tools
</a></li><li class="dropdown-item"><!----> <a href="/tools/bookmark-scripts/" class="nav-link">
  Bookmark scripts
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><span class="title">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/more/algorithm/" class="nav-link">
  算法
</a></li><li class="dropdown-item"><!----> <a href="/more/interview/" class="nav-link">
  面试题
</a></li><li class="dropdown-item"><!----> <a href="/more/hodgepodge/" class="nav-link">
  大杂烩
</a></li><li class="dropdown-item"><!----> <a href="/more/clean/" class="nav-link">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="https://v1.vuepress.vuejs.org/zh/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  VuePress1.x 官网
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>LINUX</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/os/linux/" class="sidebar-link">首页</a></li><li><a href="/os/linux/everything-is-a-file.html" class="sidebar-link">一切皆文件</a></li><li><a href="/os/linux/alias.html" class="sidebar-link">别名 alias</a></li><li><a href="/os/linux/ansible.html" class="active sidebar-link">分发脚本 ansible</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/os/linux/ansible.html#介绍" class="sidebar-link">介绍</a></li><li class="sidebar-sub-header"><a href="/os/linux/ansible.html#install" class="sidebar-link">install</a></li><li class="sidebar-sub-header"><a href="/os/linux/ansible.html#inventory" class="sidebar-link">inventory</a></li><li class="sidebar-sub-header"><a href="/os/linux/ansible.html#ad-hoc" class="sidebar-link">ad-hoc</a></li><li class="sidebar-sub-header"><a href="/os/linux/ansible.html#playbooks" class="sidebar-link">playbooks</a></li></ul></li><li><a href="/os/linux/archiving-and-compression.html" class="sidebar-link">归档和压缩</a></li><li><a href="/os/linux/command-options.html" class="sidebar-link">命令选项</a></li><li><a href="/os/linux/command-shortcut.html" class="sidebar-link">命令行快捷键</a></li><li><a href="/os/linux/crontab.html" class="sidebar-link">解放双手计划</a></li><li><a href="/os/linux/detect-your-server.html" class="sidebar-link">检测服务器通达</a></li><li><a href="/os/linux/directory-structure.html" class="sidebar-link">目录结构</a></li><li><a href="/os/linux/firewalld.html" class="sidebar-link">防火墙 firewalld</a></li><li><a href="/os/linux/gaudy-and-pragmatical-command.html" class="sidebar-link">花里胡哨且实用的命令</a></li><li><a href="/os/linux/generate-ssh-key.html" class="sidebar-link">创建 SSH Key</a></li><li><a href="/os/linux/grep-sed-awk.html" class="sidebar-link">文本三剑客</a></li><li><a href="/os/linux/how-to-enable-the-hard-disk.html" class="sidebar-link">如何启用硬盘</a></li><li><a href="/os/linux/how-to-install-linux-on-virtualbox.html" class="sidebar-link">虚拟机中安装 Linux</a></li><li><a href="/os/linux/io-redirection.html" class="sidebar-link">输入、输出重定向</a></li><li><a href="/os/linux/login-with-rsa-key.html" class="sidebar-link">使用 RSA 登录服务器</a></li><li><a href="/os/linux/raid.html" class="sidebar-link">磁盘阵列</a></li><li><a href="/os/linux/rsync-and-scp.html" class="sidebar-link">远程同步文件</a></li><li><a href="/os/linux/systemd.html" class="sidebar-link">守护系统 systemd</a></li><li><a href="/os/linux/user.html" class="sidebar-link">用户管理</a></li><li><a href="/os/linux/view-text-context.html" class="sidebar-link">查看文本内容</a></li><li><a href="/os/linux/xargs.html" class="sidebar-link">传参 xargs</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h2 id="介绍"><a href="#介绍" class="header-anchor">#</a> 介绍</h2> <p>虽然也可以自己写 Shell 实现分发脚本，但由于缺乏运维系统概念，混乱、零碎的写法总是让人难受，一接触到 ansible 就知道很难回去了。</p> <p>Ansible 是一个用来批量管理部署远程主机上服务的工具，它通过 SSH 协议实现管理节点与远程节点之间的通信，所以首先需要完成 <a href="/os/linux/login-with-rsa-key.html#生成-rsa">SSH 配置</a>。</p> <p>需要注意的是 Ansible 需要开启公钥认证，也就是添加到 <code>known_hosts</code> 中，对于大批量的操作可以禁用该行为：</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">vim</span> /etc/ansible/ansible.cfg
<span class="token comment"># 修改以下内容</span>
host_key_checking <span class="token operator">=</span> False <span class="token comment"># 取消注释</span>
</code></pre></div><h2 id="install"><a href="#install" class="header-anchor">#</a> install</h2> <p>虽然可以很简单的直接使用 <a href="https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#latest-release-via-dnf-or-yum" target="_blank" rel="noopener noreferrer">yum 安装<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>，但由于 ansible 是一个 python 编写的工具，这样就会导致 ansible 依赖于全局的 python，当全局依赖发生变化的时候可能导致 ansible 出现冲突或潜在的安全问题，所以需要隔离 ansible 与其他应用。</p> <h3 id="_1-关闭限制"><a href="#_1-关闭限制" class="header-anchor">#</a> 1. 关闭限制</h3> <div class="language-bash extra-class"><pre class="language-bash"><code>systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld <span class="token comment"># 验证</span>

<span class="token function">vim</span> /etc/sysconfig/selinux
<span class="token comment"># 修改以下内容</span>
<span class="token assign-left variable">SELINUX</span><span class="token operator">=</span>disabled

<span class="token function">reboot</span>
getenforce <span class="token comment"># 验证 Disabled</span>
</code></pre></div><h3 id="_2-安装-python"><a href="#_2-安装-python" class="header-anchor">#</a> 2. 安装 <a href="https://www.python.org/downloads/source/" target="_blank" rel="noopener noreferrer">python<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></h3> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">wget</span> https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tar.xz
<span class="token function">tar</span> xf Python-3.6.9.tar.xz
<span class="token builtin class-name">cd</span> Python-3.6.9
./configure --prefix<span class="token operator">=</span>/usr/local --with-ensurepip<span class="token operator">=</span>install --enable-shared <span class="token assign-left variable">LDFLAGS</span><span class="token operator">=</span><span class="token string">&quot;-Wl,-rpath /usr/local/lib&quot;</span>
<span class="token function">make</span> <span class="token operator">&amp;&amp;</span> <span class="token function">make</span> altinstall
<span class="token function">which</span> pip3.6
<span class="token function">ln</span> -s /usr/local/bin/pip3.6 /usr/local/bin/pip <span class="token comment"># 建立软连接</span>
pip <span class="token function">install</span> virtualenv
</code></pre></div><h3 id="_3-创建实例"><a href="#_3-创建实例" class="header-anchor">#</a> 3. 创建实例</h3> <p>需要创建一个 ansible 用户，并在该系统用户下创建一个 python3.6 版本的 virtualenv 实例。</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">useradd</span> deploy
<span class="token function">su</span> - deploy
virtualenv -p /usr/local/bin/python3.6 .py3-a2.8-env <span class="token comment"># 创建实例</span>
<span class="token builtin class-name">source</span> /home/deploy/.py3-a2.8-env/bin/activate
pip <span class="token function">install</span> paramiko PyYAML  jinja2 <span class="token comment"># 安装依赖，保持顺序</span>
</code></pre></div><h3 id="_4-安装-ansible"><a href="#_4-安装-ansible" class="header-anchor">#</a> 4. 安装 ansible</h3> <p><a href="https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#latest-release-via-dnf-or-yum" target="_blank" rel="noopener noreferrer">官网<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>推荐的安装方式，当下载速度极慢时的<a href="/tools/github/download-huge-project-from-github.html">解决方案</a>。</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">git</span> clone https://gitee.com/shanyuhai123/ansible.git
<span class="token function">mv</span> ansible .py3-a2.8-env/
<span class="token builtin class-name">cd</span> .py3-a2.8-env/ansible
<span class="token function">git</span> branch -a <span class="token comment"># 查看分支</span>
<span class="token function">git</span> checkout stable-2.8
<span class="token builtin class-name">source</span> /home/deploy/.py3-a2.8-env/ansible/hacking/env-setup -q
ansible --version <span class="token comment"># 验证</span>
</code></pre></div><h3 id="_5-启用-ansible"><a href="#_5-启用-ansible" class="header-anchor">#</a> 5. 启用 ansible</h3> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">shutdown</span> -r now <span class="token comment"># 重启</span>

<span class="token function">su</span> - deploy
<span class="token builtin class-name">source</span> .py3-a2.8-env/bin/activate <span class="token comment"># 加载 py3</span>
<span class="token builtin class-name">source</span> .py3-a2.8-env/ansible/hacking/env-setup -q <span class="token comment"># 加载 ansible</span>
ansible --version <span class="token comment"># 验证</span>
</code></pre></div><h2 id="inventory"><a href="#inventory" class="header-anchor">#</a> inventory</h2> <p>Ansible 可同事操作属于一个组的多台主机，组和主机之间的关系通过 <a href="https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html" target="_blank" rel="noopener noreferrer">inventory<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 文件配置。默认的文件路径为 <code>/etc/ansible/hosts</code>，除默认文件外，还可以使用多个 inventory 文件，当然也可以从云上拉取配置信息。</p> <h3 id="_1-ini-like"><a href="#_1-ini-like" class="header-anchor">#</a> 1. INI-like</h3> <div class="language-bash extra-class"><pre class="language-bash"><code>mail.example.com

<span class="token punctuation">[</span>t1<span class="token punctuation">]</span>
<span class="token number">192.168</span>.1.11
<span class="token number">192.168</span>.1.12

<span class="token punctuation">[</span>web<span class="token punctuation">]</span>
www<span class="token punctuation">[</span>01:50<span class="token punctuation">]</span>.example.com

<span class="token punctuation">[</span>db<span class="token punctuation">]</span>
db-<span class="token punctuation">[</span>a:f<span class="token punctuation">]</span>.example.com

<span class="token punctuation">[</span>webservers<span class="token punctuation">]</span>
foo.example.com
bar.example.com

<span class="token punctuation">[</span>dbservers<span class="token punctuation">]</span>
one.example.com
two.example.com
three.example.com:5309
</code></pre></div><h3 id="_2-yaml"><a href="#_2-yaml" class="header-anchor">#</a> 2. YAML</h3> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">all</span><span class="token punctuation">:</span>
  <span class="token key atrule">hosts</span><span class="token punctuation">:</span>
    <span class="token key atrule">mail.example.com</span><span class="token punctuation">:</span>
  <span class="token key atrule">children</span><span class="token punctuation">:</span>
    <span class="token key atrule">webservers</span><span class="token punctuation">:</span>
      <span class="token key atrule">hosts</span><span class="token punctuation">:</span>
        <span class="token key atrule">foo.example.com</span><span class="token punctuation">:</span>
        <span class="token key atrule">bar.example.com</span><span class="token punctuation">:</span>
    <span class="token key atrule">dbservers</span><span class="token punctuation">:</span>
      <span class="token key atrule">hosts</span><span class="token punctuation">:</span>
        <span class="token key atrule">one.example.com</span><span class="token punctuation">:</span>
        <span class="token key atrule">two.example.com</span><span class="token punctuation">:</span>
        <span class="token key atrule">three.example.com</span><span class="token punctuation">:</span>
</code></pre></div><h3 id="_3-inheriting-variable-values"><a href="#_3-inheriting-variable-values" class="header-anchor">#</a> 3. Inheriting variable values</h3> <p>在这种情况下自己书写规则 INI-like 风格更方便一些，但是接手他人的时候更希望看到 YAML 风格，更一目了然。</p> <ul><li><p>INI-like</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token punctuation">[</span>atlanta<span class="token punctuation">]</span>
host1
host2

<span class="token punctuation">[</span>raleigh<span class="token punctuation">]</span>
host2
host3

<span class="token punctuation">[</span>southeast:children<span class="token punctuation">]</span>
atlanta
raleigh

<span class="token punctuation">[</span>southeast:vars<span class="token punctuation">]</span>
<span class="token assign-left variable">some_server</span><span class="token operator">=</span>foo.southeast.example.com
<span class="token assign-left variable">halon_system_timeout</span><span class="token operator">=</span><span class="token number">30</span>
<span class="token assign-left variable">self_destruct_countdown</span><span class="token operator">=</span><span class="token number">60</span>
<span class="token assign-left variable">escape_pods</span><span class="token operator">=</span><span class="token number">2</span>

<span class="token punctuation">[</span>usa:children<span class="token punctuation">]</span>
southeast
northeast
southwest
northwest
</code></pre></div></li> <li><p>YAML</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">all</span><span class="token punctuation">:</span>
  <span class="token key atrule">children</span><span class="token punctuation">:</span>
    <span class="token key atrule">usa</span><span class="token punctuation">:</span>
      <span class="token key atrule">children</span><span class="token punctuation">:</span>
        <span class="token key atrule">southeast</span><span class="token punctuation">:</span>
          <span class="token key atrule">children</span><span class="token punctuation">:</span>
            <span class="token key atrule">atlanta</span><span class="token punctuation">:</span>
              <span class="token key atrule">hosts</span><span class="token punctuation">:</span>
                <span class="token key atrule">host1</span><span class="token punctuation">:</span>
                <span class="token key atrule">host2</span><span class="token punctuation">:</span>
            <span class="token key atrule">raleigh</span><span class="token punctuation">:</span>
              <span class="token key atrule">hosts</span><span class="token punctuation">:</span>
                <span class="token key atrule">host2</span><span class="token punctuation">:</span>
                <span class="token key atrule">host3</span><span class="token punctuation">:</span>
          <span class="token key atrule">vars</span><span class="token punctuation">:</span>
            <span class="token key atrule">some_server</span><span class="token punctuation">:</span> foo.southeast.example.com
            <span class="token key atrule">halon_system_timeout</span><span class="token punctuation">:</span> <span class="token number">30</span>
            <span class="token key atrule">self_destruct_countdown</span><span class="token punctuation">:</span> <span class="token number">60</span>
            <span class="token key atrule">escape_pods</span><span class="token punctuation">:</span> <span class="token number">2</span>
        <span class="token key atrule">northeast</span><span class="token punctuation">:</span>
        <span class="token key atrule">northwest</span><span class="token punctuation">:</span>
        <span class="token key atrule">southwest</span><span class="token punctuation">:</span>
</code></pre></div></li></ul> <h3 id="_4-参数"><a href="#_4-参数" class="header-anchor">#</a> 4. 参数</h3> <table><thead><tr><th style="text-align:center;">参数</th> <th>说明</th></tr></thead> <tbody><tr><td style="text-align:center;">ansible_connection</td> <td>连接类型到主机。</td></tr> <tr><td style="text-align:center;">ansible_host</td> <td>要连接的主机的名称。</td></tr> <tr><td style="text-align:center;">ansible_port</td> <td>连接端口号。</td></tr> <tr><td style="text-align:center;">ansible_user</td> <td>连接到主机时要使用的用户名。</td></tr> <tr><td style="text-align:center;">ansible_password</td> <td>身份验证的密码。</td></tr> <tr><td style="text-align:center;">ansible_ssh_private_key_file</td> <td>ssh使用的私钥文件。</td></tr> <tr><td style="text-align:center;">ansible_ssh_common_args</td> <td>此设置始终附加到sftp，scp和ssh的默认命令行。</td></tr> <tr><td style="text-align:center;">ansible_sftp_extra_args</td> <td>此设置始终附加到默认的sftp命令行。</td></tr> <tr><td style="text-align:center;">ansible_scp_extra_args</td> <td>此设置始终附加到默认scp命令行。</td></tr> <tr><td style="text-align:center;">ansible_ssh_extra_args</td> <td>此设置始终附加到默认的ssh命令行。</td></tr> <tr><td style="text-align:center;">ansible_ssh_pipelining</td> <td>确定是否使用SSH流水线。</td></tr> <tr><td style="text-align:center;">ansible_ssh_executable</td> <td>此设置将覆盖使用系统ssh的默认行为。</td></tr> <tr><td style="text-align:center;">ansible_shell_type</td> <td>目标系统的shell类型。</td></tr> <tr><td style="text-align:center;">ansible_python_interpreter</td> <td>目标主机python路径。</td></tr></tbody></table> <h2 id="ad-hoc"><a href="#ad-hoc" class="header-anchor">#</a> ad-hoc</h2> <p>ad-hoc 可以帮助完成临时任务，很多时候并不需要 Ansible 的强大力量（playbook）。</p> <p>命令格式：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>ansible <span class="token operator">&lt;</span>pattern_goes_here<span class="token operator">&gt;</span> -m <span class="token operator">&lt;</span>module_name<span class="token operator">&gt;</span> -a <span class="token operator">&lt;</span>arguments<span class="token operator">&gt;</span>
</code></pre></div><p>仅列出部分示例。</p> <h3 id="_1-command"><a href="#_1-command" class="header-anchor">#</a> 1. <a href="https://docs.ansible.com/ansible/latest/modules/command_module.html" target="_blank" rel="noopener noreferrer">command<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></h3> <p>Execute <strong>command</strong>s on targets。</p> <p>实践：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>ansible t1 -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;free -m&quot;</span>
ansible t1 -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;df -h&quot;</span>
ansible t1 -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;ls /root&quot;</span>
ansible t1 -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;cat redhat-release&quot;</span>
</code></pre></div><h3 id="_2-shell"><a href="#_2-shell" class="header-anchor">#</a> 2. <a href="https://docs.ansible.com/ansible/latest/modules/shell_module.html" target="_blank" rel="noopener noreferrer">shell<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></h3> <p>Execute shell commands on targets。</p> <p>实践：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>ansible t1 -m shell -a <span class="token string">&quot;echo hello &gt; /tmp/tmp.txt&quot;</span>
ansible t1 -m shell -a <span class="token string">&quot;cat /tmp/tmp.txt&quot;</span> <span class="token comment"># 验证</span>
</code></pre></div><h3 id="_3-copy"><a href="#_3-copy" class="header-anchor">#</a> 3. <a href="https://docs.ansible.com/ansible/latest/modules/copy_module.html" target="_blank" rel="noopener noreferrer">copy<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></h3> <p>Copy files to remote locations。</p> <p>实践：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>ansible t1 -m copy -a <span class="token string">&quot;src=/root/shell dest=/tmp&quot;</span>
ansible t1 -m shell -a <span class="token string">&quot;ls /tmp&quot;</span> <span class="token comment"># 验证</span>
ansible t1 -m shell -a <span class="token string">&quot;wc -l /tmp/shell&quot;</span> <span class="token comment"># 验证</span>
</code></pre></div><h3 id="_4-script"><a href="#_4-script" class="header-anchor">#</a> 4. <a href="https://docs.ansible.com/ansible/latest/modules/script_module.html" target="_blank" rel="noopener noreferrer">script<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></h3> <p>Runs a local script on a remote node after transferring it。</p> <p>实践：</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">echo</span> <span class="token string">&quot;echo hello&quot;</span> <span class="token operator">&gt;</span> /tmp/hello.sh
<span class="token function">cat</span> /tmp/hello.sh <span class="token comment"># 验证</span>
ansible t1 -m script -a <span class="token string">&quot;/tmp/hello.sh&quot;</span> 
</code></pre></div><h3 id="_5-file"><a href="#_5-file" class="header-anchor">#</a> 5. <a href="https://docs.ansible.com/ansible/latest/modules/file_module.html" target="_blank" rel="noopener noreferrer">file<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></h3> <p>Manage files and file properties。</p> <h3 id="_6-yum"><a href="#_6-yum" class="header-anchor">#</a> 6. <a href="https://docs.ansible.com/ansible/latest/modules/yum_module.html" target="_blank" rel="noopener noreferrer">yum<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></h3> <p>Manages packages with the yum package manager。</p> <p>实践：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>ansible webservers -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;free -m&quot;</span> <span class="token comment"># 验证</span>

ansible webservers -m yum -a <span class="token string">&quot;name=nginx state=installed&quot;</span>
ansible webservers -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;nginx -v&quot;</span> <span class="token comment"># 验证 nginx version: nginx/1.12.2</span>
<span class="token comment"># 可以注意到并非是最新版 nginx</span>
<span class="token comment"># 参照示例 https://docs.shanyuhai.top/os/centos/install-the-latest-version-of-nginx.html</span>
ansible webservers -m yum -a <span class="token string">&quot;name=yum-utils state=installed&quot;</span>
ansible webservers -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;ll /etc/yum.repos.d/nginx.repo&quot;</span> <span class="token comment"># 验证</span>
<span class="token function">vim</span> /etc/yum.repos.d/nginx.repo
<span class="token function">cat</span> /etc/yum.repos.d/nginx.repo <span class="token comment"># 验证</span>
<span class="token comment"># 追加内容参考示例</span>
ansible webservers -m copy -a <span class="token string">&quot;src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/nginx.repo&quot;</span>
ansible webservers -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;cat /etc/yum.repos.d/nginx.repo&quot;</span> <span class="token comment"># 验证</span>
ansible webservers -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;yum-config-manager --enable nginx-mainline&quot;</span>
ansible webservers -m yum -a <span class="token string">&quot;name=nginx state=installed&quot;</span> <span class="token comment"># 更新</span>
<span class="token comment"># installed 测试时未能更新，可能需要 present 参数</span>
<span class="token comment"># 不推荐使用 yum 卸载</span>
ansible webservers -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;nginx -v&quot;</span> <span class="token comment"># 验证 </span>
</code></pre></div><h3 id="_7-systemd"><a href="#_7-systemd" class="header-anchor">#</a> 7. <a href="https://docs.ansible.com/ansible/latest/modules/systemd_module.html" target="_blank" rel="noopener noreferrer">systemd<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></h3> <p>Manage services。</p> <p>实践：</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 最好将服务写完整</span>
ansible t1 -m systemd -a <span class="token string">&quot;name=crond.service  enabled=yes state=started&quot;</span>
ansible t1 -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;systemctl status crond&quot;</span> <span class="token comment"># 验证</span>
</code></pre></div><h3 id="_8-cron"><a href="#_8-cron" class="header-anchor">#</a> 8. <a href="https://docs.ansible.com/ansible/latest/modules/cron_module.html" target="_blank" rel="noopener noreferrer">cron<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></h3> <p>Manage cron.d and crontab entries。</p> <p>实践：</p> <div class="language-bash extra-class"><pre class="language-bash"><code>ansible webservers -m <span class="token function">cron</span> -a <span class="token string">&quot;name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov &gt; /dev/null 2&gt;&amp;1'&quot;</span> 
ansible webservers -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;crontab -l&quot;</span> <span class="token comment"># 验证</span>

<span class="token comment"># 删除任务</span>
ansible webservers -m <span class="token function">cron</span> -a <span class="token string">&quot;name='sync time' state=absent&quot;</span> 
ansible webservers -m <span class="token builtin class-name">command</span> -a <span class="token string">&quot;crontab -l&quot;</span> <span class="token comment"># 验证</span>
</code></pre></div><h2 id="playbooks"><a href="#playbooks" class="header-anchor">#</a> playbooks</h2> <p>相对于 ad-hoc，playbooks 则强大的多，当然也复杂得多。playbook 由一个或多个 plays 组成，在 play 中，一组机器被映射为定义好的角色，其内容被称为 tasks（任务），在基本层次的应用中，一个任务是一个对 ansible 模块的调用。</p> <p>对于如何良好的实践这些内容，有一些整套的 <a href="https://github.com/ansible/ansible-examples" target="_blank" rel="noopener noreferrer">playbooks<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。</p> <h3 id="_1-one-play"><a href="#_1-one-play" class="header-anchor">#</a> 1. one play</h3> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token punctuation">---</span>
<span class="token punctuation">-</span> <span class="token key atrule">hosts</span><span class="token punctuation">:</span> webservers
  <span class="token key atrule">vars</span><span class="token punctuation">:</span>
    <span class="token key atrule">http_port</span><span class="token punctuation">:</span> <span class="token number">80</span>
    <span class="token key atrule">max_clients</span><span class="token punctuation">:</span> <span class="token number">200</span>
  <span class="token key atrule">remote_user</span><span class="token punctuation">:</span> root
  <span class="token key atrule">tasks</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> ensure apache is at the latest version
    <span class="token key atrule">yum</span><span class="token punctuation">:</span>
      <span class="token key atrule">name</span><span class="token punctuation">:</span> httpd
      <span class="token key atrule">state</span><span class="token punctuation">:</span> latest
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> write the apache config file
    <span class="token key atrule">template</span><span class="token punctuation">:</span>
      <span class="token key atrule">src</span><span class="token punctuation">:</span> /srv/httpd.j2
      <span class="token key atrule">dest</span><span class="token punctuation">:</span> /etc/httpd.conf
    <span class="token key atrule">notify</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> restart apache
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> ensure apache is running
    <span class="token key atrule">service</span><span class="token punctuation">:</span>
      <span class="token key atrule">name</span><span class="token punctuation">:</span> httpd
      <span class="token key atrule">state</span><span class="token punctuation">:</span> started
  <span class="token key atrule">handlers</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> restart apache
      <span class="token key atrule">service</span><span class="token punctuation">:</span>
        <span class="token key atrule">name</span><span class="token punctuation">:</span> httpd
        <span class="token key atrule">state</span><span class="token punctuation">:</span> restarted
</code></pre></div><h3 id="_2-multiple-plays"><a href="#_2-multiple-plays" class="header-anchor">#</a> 2. multiple plays</h3> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token punctuation">---</span>
<span class="token punctuation">-</span> <span class="token key atrule">hosts</span><span class="token punctuation">:</span> webservers
  <span class="token key atrule">remote_user</span><span class="token punctuation">:</span> root

  <span class="token key atrule">tasks</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> ensure apache is at the latest version
    <span class="token key atrule">yum</span><span class="token punctuation">:</span>
      <span class="token key atrule">name</span><span class="token punctuation">:</span> httpd
      <span class="token key atrule">state</span><span class="token punctuation">:</span> latest
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> write the apache config file
    <span class="token key atrule">template</span><span class="token punctuation">:</span>
      <span class="token key atrule">src</span><span class="token punctuation">:</span> /srv/httpd.j2
      <span class="token key atrule">dest</span><span class="token punctuation">:</span> /etc/httpd.conf

<span class="token punctuation">-</span> <span class="token key atrule">hosts</span><span class="token punctuation">:</span> databases
  <span class="token key atrule">remote_user</span><span class="token punctuation">:</span> root

  <span class="token key atrule">tasks</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> ensure postgresql is at the latest version
    <span class="token key atrule">yum</span><span class="token punctuation">:</span>
      <span class="token key atrule">name</span><span class="token punctuation">:</span> postgresql
      <span class="token key atrule">state</span><span class="token punctuation">:</span> latest
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> ensure that postgresql is started
    <span class="token key atrule">service</span><span class="token punctuation">:</span>
      <span class="token key atrule">name</span><span class="token punctuation">:</span> postgresql
      <span class="token key atrule">state</span><span class="token punctuation">:</span> started
</code></pre></div><h3 id="_3-directory-layout"><a href="#_3-directory-layout" class="header-anchor">#</a> 3. Directory Layout</h3> <div class="language-yaml extra-class"><pre class="language-yaml"><code>production                <span class="token comment"># inventory file for production servers</span>
staging                   <span class="token comment"># inventory file for staging environment</span>

group_vars/
   group1.yml             <span class="token comment"># here we assign variables to particular groups</span>
   group2.yml
host_vars/
   hostname1.yml          <span class="token comment"># here we assign variables to particular systems</span>
   hostname2.yml

library/                  <span class="token comment"># if any custom modules, put them here (optional)</span>
module_utils/             <span class="token comment"># if any custom module_utils to support modules, put them here (optional)</span>
filter_plugins/           <span class="token comment"># if any custom filter plugins, put them here (optional)</span>

site.yml                  <span class="token comment"># master playbook</span>
webservers.yml            <span class="token comment"># playbook for webserver tier</span>
dbservers.yml             <span class="token comment"># playbook for dbserver tier</span>

roles/
    common/               <span class="token comment"># this hierarchy represents a &quot;role&quot;</span>
        tasks/            <span class="token comment">#</span>
            main.yml      <span class="token comment">#  &lt;-- tasks file can include smaller files if warranted</span>
        handlers/         <span class="token comment">#</span>
            main.yml      <span class="token comment">#  &lt;-- handlers file</span>
        templates/        <span class="token comment">#  &lt;-- files for use with the template resource</span>
            ntp.conf.j2   <span class="token comment">#  &lt;------- templates end in .j2</span>
        files/            <span class="token comment">#</span>
            bar.txt       <span class="token comment">#  &lt;-- files for use with the copy resource</span>
            foo.sh        <span class="token comment">#  &lt;-- script files for use with the script resource</span>
        vars/             <span class="token comment">#</span>
            main.yml      <span class="token comment">#  &lt;-- variables associated with this role</span>
        defaults/         <span class="token comment">#</span>
            main.yml      <span class="token comment">#  &lt;-- default lower priority variables for this role</span>
        meta/             <span class="token comment">#</span>
            main.yml      <span class="token comment">#  &lt;-- role dependencies</span>
        library/          <span class="token comment"># roles can also include custom modules</span>
        module_utils/     <span class="token comment"># roles can also include custom module_utils</span>
        lookup_plugins/   <span class="token comment"># or other types of plugins, like lookup in this case</span>

    webtier/              <span class="token comment"># same kind of structure as &quot;common&quot; was above, done for the webtier role</span>
    monitoring/           <span class="token comment"># &quot;&quot;</span>
    fooapp/               <span class="token comment"># &quot;&quot;</span>
</code></pre></div><p>备用方案：</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code>inventories/
   production/
      hosts               <span class="token comment"># inventory file for production servers</span>
      group_vars/
         group1.yml       <span class="token comment"># here we assign variables to particular groups</span>
         group2.yml
      host_vars/
         hostname1.yml    <span class="token comment"># here we assign variables to particular systems</span>
         hostname2.yml

   staging/
      hosts               <span class="token comment"># inventory file for staging environment</span>
      group_vars/
         group1.yml       <span class="token comment"># here we assign variables to particular groups</span>
         group2.yml
      host_vars/
         stagehost1.yml   <span class="token comment"># here we assign variables to particular systems</span>
         stagehost2.yml

library/
module_utils/
filter_plugins/

site.yml
webservers.yml
dbservers.yml

roles/
    common/
    webtier/
    monitoring/
    fooapp/
</code></pre></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2020年3月17日星期二下午2点47分</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/os/linux/alias.html" class="prev">
        别名 alias
      </a></span> <span class="next"><a href="/os/linux/archiving-and-compression.html">
        归档和压缩
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.1f0f93c9.js" defer></script><script src="/assets/js/2.f28f3227.js" defer></script><script src="/assets/js/97.65a03aad.js" defer></script><script src="/assets/js/3.a7eaa85f.js" defer></script>
  </body>
</html>
